import formData from './components/fomData.vue'
import topStep from './components/topStep.vue'
import formDataPdf from './components/fomDataPdf.vue'
import axios from "axios";

export default {
    components: {
        formData,
        topStep,
        formDataPdf
    },
    props: {
        pdfParams: {
            type: Object,
            default: () => {
                return {}
            }
        }
    },
    inject: ["imageUrl", "serviceUrl"],
    data() {
        return {
            pdfData: [],
            pdfParams: {},
            pdfTitleSub: "",
            pdfTitle: "",
            props: {
                checkStrictly: true,
                expandTrigger: "click",
                value: "key",
                label: "title",
            },
            tableLoading: false, // 控制表格加载状态

            saisId: null,
            userInfo: JSON.parse(localStorage.getItem("userInfo")),
            form: {
                name: ''
            },
            headers: {
                "X-Access-Token": localStorage.getItem("token"),
            },
            tableData: [],
            pageNo: 1,
            pageSize: 20,
            total: 0,
            params: {},//每个页面的参数
            pdfStatic: false,
            pingsztlb: [],
            saisjclb: [],
            options: [],
            searchFormData: {pingszt: ''},
            formData: {},
            soustjList: [],
            cascaderData: {},
            btnLoading:false,
        }
    },
    mounted() {
        this.searchFormData = JSON.parse(localStorage.getItem('searchFormData'))
    },
    methods: {
        cascaderChange(e) {
            if (e) {
                this.formData.saiq = e[e.length - 1];
            }
            this.getData()
        },
        cascaderChangeData(e, type) {
            console.log(e, type, 'cascaderChangeData');
            if (e) {
                this.formData[type] = e[e.length - 1];
            }
            this.getData()
        },
        getIndex(index) {
            return (this.pageNo - 1) * this.pageSize + index + 1;
        },
        //动态搜索列表
        getDongtsstjlb(id) {
            let that = this;
            axios
                .get(that.serviceUrl + "/saisjczd2/cbda2Saisjczd/getDongtsstjlb", {
                    headers: {
                        "X-Access-Token": localStorage.getItem("token"),
                    },
                    params: {
                        saisId: id,
                        _t: new Date().getTime(),
                    },
                })
                .then((resp) => {
                    console.log(resp, '*****-----*****');
                    if (resp && resp.data.code == 200) {
                        resp.data.result.forEach(async (item, index) => {
                            if (item.zidbm && item.zidbm != '') {
                                await this.getCascaderData(item.zidbm, item.zidmc);
                            }
                            if (this.searchFormData && Object.keys(this.searchFormData).length != 0) {
                                this.formData[item.zidmc] = this.searchFormData[item.zidmc] ? this.searchFormData[item.zidmc] : ''
                            } else {
                                this.formData[item.zidmc] = ''
                            }
                        })
                        this.soustjList = resp.data.result;
                        if (this.searchFormData && Object.keys(this.searchFormData).length != 0) {
                            this.formData.pingszt = this.searchFormData.pingszt ? this.searchFormData.pingszt : ''
                            this.pageNo = this.searchFormData.pageNo ? this.searchFormData.pageNo : 1
                        }
                        console.log(this.soustjList, 'this.soustjList');
                    }
                })
                .catch((error) => {
                    //console.log(error);
                });

        },
        // 赛事状态
        dictItemList(num) {
            let that = this
            axios
                .get(that.serviceUrl + "/sys/dictItem/list", {
                    headers: {
                        "X-Access-Token": localStorage.getItem("token"),
                    },
                    params: {
                        dictId: num,
                        _t: new Date().getTime(),
                    },
                })
                .then((resp) => {
                    // let all = [{itemValue: '', itemText: '全部'}]
                    this.pingsztlb = resp.data.result.records;
                })
                .catch((error) => {
                    //console.log(error);
                });
        },
        // 级联控件数据
        getCascaderData(zidbm, name) {
            axios
                .get(this.serviceUrl + "/sys/category/loadTreeRoot", {
                    headers: {
                        "X-Access-Token": localStorage.getItem("token"),
                    },
                    params: {
                        async: false,
                        pcode: zidbm,
                        _t: new Date().getTime(),
                    },
                })
                .then((resp) => {
                    this.cascaderData[name] = resp.data.result;
                })
                .catch((error) => {
                    //console.log(error);
                });
        },
        // 级联控件数据
        getOptionsData(zidbm) {
            let that = this
            axios
                .get(that.serviceUrl + "/sys/category/loadTreeRoot", {
                    headers: {
                        "X-Access-Token": localStorage.getItem("token"),
                    },
                    params: {
                        async: false,
                        pcode: zidbm,
                        _t: new Date().getTime(),
                    },
                })
                .then((resp) => {
                    this.options = resp.data.result;
                })
                .catch((error) => {
                    //console.log(error);
                });
        },
        cdba2SaisjcData() {
            let that = this
            axios
                .get(that.serviceUrl + "/saisjc2/cdba2Saisjc/list", {
                    headers: {
                        "X-Access-Token": localStorage.getItem("token"),
                    },
                    params: {
                        saisId: this.saisId,
                        _t: new Date().getTime(),
                    },
                })
                .then((resp) => {
                    this.saisjclb = resp.data.result.records;
                })
                .catch((error) => {
                    //console.log(error);
                });
        },
        async getPdf(item) {
            await this.submit(2)
            // setTimeout(()=>{
            //   this.zuopinExportPdf()
            // },500)
        },
        handleSizeChange() {
            this.getData()
        },
        handleCurrentChange(v) {
            this.pageNo = v;
            this.getData(v)
        },
        zuopinExportPdf(data) {
            this.$refs["formDataRef"].btnLoading = true;
            let that = this
            const config = {
                headers: {
                    "X-Access-Token": localStorage.getItem("token"),
                },
            };
            let params = {
                dongtzpb: data.dongtzpb,
                id: data.id,
                saisId: data.saisId,
                saisjcId: data.saisjcId,
            }
            console.log(params, 'params');
            setTimeout(() => {
                axios
                    .post(
                        that.serviceUrl + "/saisjczd2/cbda2Saisjczd/zuopinExportPdf", params, config
                    )
                    .then((resp) => {
                        if (resp.data.success) {

                            this.pdfTitle = resp.data.result.mingc
                            this.pdfTitleSub = resp.data.result.dassm ? resp.data.result.dassm : resp.data.result.mingc
                            let allData = []
                            let endData = []
                            //item.biaodkjlx == '9' 视频附件 || item.biaodkjlx == '6' 图片 || item.biaodkjlx == '5' || item.biaodkjlx == '10'
                            allData = resp.data.result.list
                            allData.forEach(item => {
                                if ((item.biaodkjlx == '6' || item.biaodkjlx == '9' || item.biaodkjlx == '10') && item.ziduanValue.includes('[') && item.ziduanValue.includes(']')) {
                                    item.biaodkjlx = eval('(' + item.biaodkjlx + ')')
                                }
                                endData.push({
                                    // col: item.fenl24,
                                    col: 24,
                                    type: item.biaodkjlx,
                                    title: item.ziduanzwm,
                                    ...item.biaodkjlx == '10' ? {header: eval('(' + item.kongjsj + ')')} : {},
                                    value: (item.biaodkjlx == '5' || item.biaodkjlx == '9' || item.biaodkjlx == '10') && item.ziduanValue.includes('[') && item.ziduanValue.includes(']') ? eval('(' + item.ziduanValue + ')') : item.ziduanValue
                                })
                            })
                            console.log(allData, 'allData');
                            console.log(endData, 'endData');
                            this.pdfStatic = true;
                            this.$refs["formDataPdfRef"].formData = endData
                            setTimeout(() => {
                                this.$refs["formDataRef"].btnLoading = false;
                                this.$refs["formDataPdfRef"].printOutPdf(resp.data.result.mingc);
                                this.pdfStatic = false;
                            }, 100);
                        }
                    })
                    .catch((error) => {
                        // this.$message.warning(error)
                    });
            }, 500)
        },
        tableRowClassName(row, rowIndex) {
            return rowIndex % 2 === 0 ? 'even-row' : 'odd-row';
        }
    }
}